package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import com.fz.us.admin.company.dao.PowerDao;
import com.fz.us.admin.company.entity.Department;
import com.fz.us.admin.company.entity.Post;
import com.fz.us.admin.company.entity.Power;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Dao实现类 - 职权
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-11 。
 * ============================================================================
 */
@Repository
public class PowerDaoImpl extends BaseDaoImpl<Power, String> implements PowerDao {

    @Override
    public List<Power> getByDepartId(String departId) {
        String hql="from Power as p where p.department.id=? and p.state=?";
        return (List<Power>)getSession().createQuery(hql).setParameter(0,departId).setParameter(1, BaseEnumManager.StateEnum.Enable).list();
    }

    @Override
    public Power getPowerByDepartAndPost(Department department, Post post) {
        String hql="from Power as p where p.department.id=? and p.post.id=? and p.state=?";
        List<Power> powerList = getSession().createQuery(hql).setParameter(0,department.getId()).setParameter(1,post.getId()).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        if(powerList.size()>0){
            return powerList.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Power> getAllPowers(Company company) {
        Assert.notNull(company, "company is required");
        String hql = "from " + Power.class.getName() + " model where model.company = :company and model.state = :state order by model.department.sortNo, model.post.sortNo asc ";
        return getSession().createQuery(hql).setParameter("company", company).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Power> getRootPowers(Company company) {
        Assert.notNull(company, "company is required");
        String hql = "from " + Power.class.getName() + " model where model.company = :company and model.parent is null and model.state = :state order by model.department.sortNo, model.post.sortNo asc ";
        return getSession().createQuery(hql).setParameter("company", company).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Power> getPowers(Company company, Power power) {
        Assert.notNull(company, "company is required");
        Assert.notNull(power, "power is required");
        String hql = "from " + Power.class.getName() + " model where model.company = :company and model.parent = :parent and model.state = :state order by model.department.sortNo, model.post.sortNo asc ";
        return getSession().createQuery(hql).setParameter("company", company).setParameter("parent", power).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Power> getLevelPowers(Company company, Department department) {
        Assert.notNull(company, "company is required");
        Assert.notNull(department, "department is required");
        String hql = "from " + Power.class.getName() + " model where model.company = :company and model.department.level <= :level and model.state = :state order by model.department.sortNo, model.post.sortNo asc ";
        return getSession().createQuery(hql).setParameter("company", company).setParameter("level", department.getLevel()).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @Override
    public boolean isParentPower(Power power) {
        Assert.notNull(power, "power is required");
        String hql = "select count(*) from " + Power.class.getName() + " model where model.parent = :parent and model.state = :state";
        return ((Long) getSession().createQuery(hql).setParameter("parent", power).setParameter("state", BaseEnumManager.StateEnum.Enable).uniqueResult()) > 0;
    }

    @Override
    public boolean hasPost(Post post) {
        Assert.notNull(post, "post is required");
        String hql = "select count(*) from " + Power.class.getName() + " model where model.post = :post and ( model.state = :state1 or model.state = :state2 )";
        return ((Long) getSession().createQuery(hql).setParameter("post", post).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult()) > 0;
    }

    @Override
    public boolean hasDepartment(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "select count(*) from " + Power.class.getName() + " model where model.department = :department and ( model.state = :state1 or model.state = :state2 )";
        return ((Long) getSession().createQuery(hql).setParameter("department", department).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult()) > 0;
    }

    @Override
    //要舍弃
    public Power getPowerByValue(Company company, String value) {
        Assert.notNull(company, "company is required");
        Assert.hasText(value, "value is required");

        String hql = "from " + Power.class.getName() + " model where model.company = :company and model.value = :value and ( model.state = :state1 or model.state = :state2 )";
        return (Power) getSession().createQuery(hql).setParameter("company", company).setParameter("value", value).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult();
    }

}